﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;

using Ivy.SiteAdmin.Entity;
using Ivy.SiteAdmin.IBusinessLogic;
using Ivy.SiteAdmin.BusinessLogic;

namespace Ivy.Common.Framework.GeneralUtility
{
    public enum ScreenActionMode
    {
        UnKnown = 0,
        Display = 1,
        New = 2,
        Edit = 3
    }

    public class ExceptionUtility
    {
        #region  Variable & constant
        const string constExceptionLoggerOn = "ExceptionLoggerOn";
        const string constExceptionLogFileName = "ExceptionLogFileName";
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        static public void ExceptionHandler(Exception ex, string pServerRootPath)
        {
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string lIsExceptionLoggerOn = _AppSettingsReader.GetValue(constExceptionLoggerOn, typeof(string)).ToString();
            string lFullPathLogFileName = _AppSettingsReader.GetValue(constExceptionLogFileName, typeof(string)).ToString();
            if (lIsExceptionLoggerOn == "True")
            {
                if (lFullPathLogFileName != "DataBase")
                {
                    lFullPathLogFileName = pServerRootPath + _AppSettingsReader.GetValue(constExceptionLogFileName, typeof(string)).ToString();
                    string lLogText = string.Empty;
                    lLogText = "=========================================\r\n" + DateTime.Now + " , \r\n[APPLICATION]:\t" + ex.GetType().ToString() + " - " + ex.Source + " , \r\n[METHOD]:\t" + ex.TargetSite.ToString() + " , \r\n[MESSAGE]:\t" + ex.Message + " , \r\n[STACKTRACE]:\t" + ex.StackTrace + "\r\n=========================================\r\n";
                    FileUtility lFileUtility = new FileUtility();
                    lFileUtility.WriteToLogFile(lLogText, lFullPathLogFileName);
                }
                else
                {
                    Sys_ErrorLog lSys_ErrorLog = new Sys_ErrorLog();
                    SiteAdminContentManagementBus lSiteAdminContentManagementBus = new SiteAdminContentManagementBus();
                    lSys_ErrorLog.ApplicationName = ex.GetType().ToString() + " - " + ex.Source;
                    lSys_ErrorLog.MethodName = ex.TargetSite.ToString();
                    lSys_ErrorLog.ErrorMessage = ex.Message;
                    lSys_ErrorLog.StackTrace = ex.StackTrace;
                    lSys_ErrorLog.Sys_CreatedByID = "-1";
                    lSiteAdminContentManagementBus.SaveSys_ErrorLogDetails(lSys_ErrorLog);
                }
            }
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        #endregion Private Methods
    }
}
